# BSD3.0
# cili@163.com


import tkinter as tk
from tkinter import ttk
import jieba

# 分词器
def cut(keyword):
    result = [keyword]
    segs = jieba.cut_for_search(keyword)
    for seg in segs:
        if seg not in result and len(seg) > 1:
            result.append(seg)
    return result

if __name__ == "__main__":
    root = tk.Tk()
    root.title("分词测试")

    def click_button():
        content = spider_text.get().strip()
        segs = jieba.cut(content, cut_all=False)
        seg_result = "/".join(segs)
        content_text.delete(0, "end")
        content_text.insert(0, seg_result)

    spider_label = ttk.Label(root, text="输入短语：")
    spider_label.grid(row=0, column=0, sticky="w", padx=5, pady=5)
    spider_text = ttk.Entry(root, width=50)
    spider_text.insert(0, '北斗卫星导航系统')
    spider_text.grid(row=1, column=0, sticky="w", padx=5, pady=5)
    spider_button = ttk.Button(root, text="分词", command=click_button)
    spider_button.grid(row=1, column=2, padx=10, sticky="w", pady=5)
    content_label = ttk.Label(root, text="运行结果：")
    content_label.grid(row=2, column=0, sticky="w", padx=5, pady=5)
    content_text = ttk.Entry(root, width=50)
    content_text.grid(row=3, column=0, sticky="w",padx=5, pady=5, columnspan=3)

    root.update_idletasks()
    window_width = root.winfo_width()
    window_height = root.winfo_height()
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    x = int((screen_width - window_width) / 2)
    y = int((screen_height - window_height) / 2)
    root.geometry(f"+{x}+{y}")

    root.mainloop()